package com.ukao.cashregister.base;

import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.ColorRes;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.ukao.cashregister.retrofit.ApiClient;
import com.ukao.cashregister.retrofit.ApiStores;
import com.ukao.cashregister.utils.T;
import com.ukao.cashregister.widget.MyGridLayoutManager;
import com.ukao.cashregister.widget.ZProgressHUD;

import org.greenrobot.eventbus.EventBus;

import cn.jiguang.analytics.android.api.JAnalyticsInterface;
import me.yokeyword.fragmentation.SupportFragment;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by ukaodev on 2017/5/24.
 */

public  abstract class   BaseFragment   extends SupportFragment implements DialogInterface.OnDismissListener {
    protected static final String ARG_PARAM1 = "param1";
    protected static final String ARG_PARAM2 = "param2";
    protected static final String ARG_PARAM3 = "param3";
    protected static final String ARG_PARAM4 = "param4";
    protected static final String ARG_PARAM5 = "param5";
    protected static final String ARG_PARAM6 = "param6";
    protected Context activity;
    protected Resources resources;
    protected ZProgressHUD progressDialog;
    protected boolean isVisible;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        activity=context;


    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        resources = getActivity().getResources();
        if (useEventBus()) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onSupportInvisible() {
        super.onSupportInvisible();
        isVisible=false;
        hideSoftInput();
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        isVisible=true;
    }

    public Resources getResource() {
        return resources;
    }

    @Override
    protected void hideSoftInput() {
        super.hideSoftInput();
    }



    public  void hideKeyboard(View mView ) {
        InputMethodManager imm = (InputMethodManager)
                getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mView.getWindowToken(), 0);


    }
    protected abstract void initView();


    protected void initListener() {

    }

    public  int  getColors( @ColorRes int id){
        return   ContextCompat.getColor(_mActivity,id);

    }
    protected void initLinearRecyclerView(RecyclerView recyclerViews ) {
        recyclerViews.setHasFixedSize(true);
        recyclerViews.setLayoutManager(new LinearLayoutManager(activity));


    }

    protected void initGalleryRecyclerView(RecyclerView recyclerViews) {
        recyclerViews.setHasFixedSize(true);
        LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(activity);
        recyclerViews.setLayoutManager(mLinearLayoutManager);
        mLinearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);



    }


    protected void initGridRecyclerView(RecyclerView recyclerViews, int num) {
        recyclerViews.setHasFixedSize(true);
        recyclerViews.setLayoutManager(new MyGridLayoutManager(activity, num));


    }


    private CompositeSubscription mCompositeSubscription;

    public ApiStores apiStores() {
        return ApiClient.retrofit().create(ApiStores.class);
    }

    public void addSubscription(Observable observable, Subscriber subscriber) {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }
        mCompositeSubscription.add(observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber));
    }

    public void onUnsubscribe() {

        //取消注册，以避免内存泄露
        if (mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions())
            mCompositeSubscription.unsubscribe();
    }


    protected String getText(TextView txt) {
        String message = txt.getText().toString().trim();

        return message;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        progressDialog=null;
        onUnsubscribe();
        mCompositeSubscription=null;
        if (useEventBus()) {
            EventBus.getDefault().unregister(this);
        }
    }
    protected  boolean useEventBus(){
        return false;
    }
    public void finish(){
        pop();
    }

    public ZProgressHUD showProgressDialog() {
        if (progressDialog==null){
            progressDialog = new ZProgressHUD(_mActivity);
            progressDialog.setOnDismissListener(this);
        }
          progressDialog.show();

        return progressDialog;
}

    public ZProgressHUD showProgressDialog(String message) {
        if (progressDialog==null){
            progressDialog = new ZProgressHUD(_mActivity);
            progressDialog.setOnDismissListener(this);
        }

        progressDialog.setMessage(message);
        progressDialog.show();
        return progressDialog;
    }




    public void dismissProgressDialog() {
        if (progressDialog != null && progressDialog.isShowing()) {
            // progressDialog.hide();会导致android.view.WindowLeaked
            progressDialog.dismiss();
        }
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        onUnsubscribe();
        mCompositeSubscription=null;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isResumed()) {
            if(isVisibleToUser){
                JAnalyticsInterface.onPageStart(getActivity(),this.getClass().getCanonicalName());
            }else {
                JAnalyticsInterface.onPageEnd(getActivity(),this.getClass().getCanonicalName());
            }
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);

        if(hidden){
            JAnalyticsInterface.onPageEnd(getActivity(),this.getClass().getCanonicalName());
        }else {
            JAnalyticsInterface.onPageStart(getActivity(),this.getClass().getCanonicalName());
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!isHidden() && getUserVisibleHint()) {

            JAnalyticsInterface.onPageStart(getActivity(),this.getClass().getCanonicalName());
        }
    }

    @Override
    public void onPause() {
        super.onPause();

        if (!isHidden() && getUserVisibleHint()) {
            JAnalyticsInterface.onPageEnd(getActivity(),this.getClass().getCanonicalName());
        }
    }




}
